using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace Dude.MLS
{
    /// <summary>
    /// Language functions
    /// </summary>
    public static class Lang
    {
        internal static string currentLang = SysLang;
        internal static string[] localePriority = new string[] { currentLang, currentLang.Split('-')[0], "en" };

        public static string SysLang
        {
            get
            {
                return System.Globalization.CultureInfo.CurrentCulture.Name;
            }
        }

        private static LocaleModule currentModule;
        private static LocaleModule CurrentModule
        {
            get
            {
                return currentModule ?? (currentModule = LocaleModule.GetModule(currentLang, null));
            }
            set
            {
                currentModule = value;
            }
        }

        /// <summary>
        /// Get or sets locale priority codes. First is set as current language code.
        /// </summary>
        public static string[] LocalePriority
        {
            get
            {
                return localePriority;
            }
            set
            {
                localePriority = value;
                CurrentModule = null;
                currentLang = value[0];
            }
        }

        /// <summary>
        /// Translates string from catalog to selected language
        /// </summary>
        /// <param name="stringName">lookup of resource in catalog</param>
        /// <returns>translated string</returns>
        public static string GetText(string stringName)
        {
            return CurrentModule.GetText(stringName);
        }

        public static string GetText(string stringName, params string[] args)
        {
            return String.Format(CurrentModule.GetText(stringName), args);
        }

        /// <summary>
        /// Get localized text for module
        /// </summary>
        /// <param name="module">name of module</param>
        /// <param name="stringName"></param>
        /// <returns></returns>
        public static string GetModuleText(string module, string stringName)
        {
            return LocaleModule.GetModule(currentLang, module).GetText(stringName);
        }

        public static string GetModuleText(string module, string stringName, params string[] args)
        {
            return String.Format(LocaleModule.GetModule(currentLang,module).GetText(stringName), args);
        }

        [Obsolete("Use LocalizeUI(Control)")]
        public static void LocalizeUI(string module, Control control)
        {
            string s = control.Text;
            if ((s == null) || (s == "")) s = control.Name;
            control.Text = GetModuleText(module, s);

            foreach (Control subcontrol in control.Controls)
            {
                if (subcontrol is TextBoxBase) continue;
                if (subcontrol is ListControl) continue;

                LocalizeUI(module, subcontrol);
            }
        }

        public static void LocalizeUI(Type type, Control control)
        {
            string s = control.Text;
            if ((s != null) && (s != ""))
            {
                control.Text = GetModuleText(type.Name, s);
            }

            foreach (Control subcontrol in control.Controls)
            {
                if (subcontrol is TextBoxBase) continue;
                if (subcontrol is ListControl) continue;

                LocalizeUI(type, subcontrol);
            }
        }

        public static void LocalizeUI(Control control)
        {
            LocalizeUI(control.GetType(), control);
        }
    }
}
